import urllib.request
import urllib.parse
import json

url = "https://fanyi.baidu.com/v2transapi?from=en&to=zh"
headers = {
    # 'Accept': '*/*',
    # 'Accept-Encoding': 'gzip, deflate, br',
    # 'Accept-Language': 'zh-CN,zh;q=0.9',
    # 'Connection': 'keep-alive',
    # 'Content-Length': '135',
    # 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie':'BIDUPSID=726ED48D26E6989FC91FAD7B557A714A; PSTM=1554035804; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BAIDUID=C8E2C4200BB6EDADCA1F0AFEE8C1C80B:FG=1; __yjs_duid=1_91d9aec532a817856059ca2a24ae0aa01620138475079; BDUSS=hzMTF0QkI5S0lCdHZsN0tYVi1nY0xHSTU4Q2NCUGd3NVhQOVUzWHpJVnNWamxoRVFBQUFBJCQAAAAAAAAAAAEAAAD94bTLZ292ZXJub3JfMTEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGzJEWFsyRFhTn; BDUSS_BFESS=hzMTF0QkI5S0lCdHZsN0tYVi1nY0xHSTU4Q2NCUGd3NVhQOVUzWHpJVnNWamxoRVFBQUFBJCQAAAAAAAAAAAEAAAD94bTLZ292ZXJub3JfMTEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGzJEWFsyRFhTn; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDSFRCVID_BFESS=eCFOJeC626FzRC7H8biawNlHzgZEO6rTH6ao7fqjaayG0h3HiUwaEG0POx8g0Ku-c1qlogKK0mOTHv-F_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF_BFESS=tR4DoDIKtD-3DjrR5tTS-tCOKgT22-us5m7l2hcH0KLKMlQu0x-by5kVMfQfWt77MDvzKlAK-fb1MRjvjPjaM4__2ttjaTJ72GTf3l5TtUJcJKnTDMRhqtJLKfvyKMnitIj9-pnKHlQrh459XP68bTkA5bjZKxtq3mkjbPbDfn02eCKu-n5jHj3QeaDj3D; BAIDUID_BFESS=C8E2C4200BB6EDADCA1F0AFEE8C1C80B:FG=1; delPer=0; PSINO=2; H_PS_PSSID=34650_34440_34536_34068_31254_34553_33848_34585_34106_34578_26350_34627_34416_34710_22159_34702_34676; BA_HECTOR=81ag2104al852025is1gkb0vf0q; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1631618661,1631771563,1631946296; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1631946309; ab_sr=1.0.1_NTU3OWQ5ZThjYWZiY2ZiMWY3NTM5OTI2YTk4MzA0ZmUyYTAyOWI0ZWYzNmY1ZGU4OTIxNzE5NWQ3OWMzMDlmNGRiZjM5MTRhYjU4Mzk4M2ZmMmRkYTUyM2YwNmMxZTQ1ZDM4ODFlYmVmYmQ3MGRiNmQwY2E2Mjc4NjJiODhlYmIxNDM3Y2Q3YWQzMTBlZWM2Mzk5YzFiNTBiYjUyODFkNThjMWNlOTFhYzZmOGY1NmFhNmZkOTE4YTllYmI2M2E4',
    # 'Host': 'fanyi.baidu.com',
    # 'Origin': 'https://fanyi.baidu.com',
    # 'Referer': 'https://fanyi.baidu.com/?aldtype=16047',
    # 'sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
    # 'sec-ch-ua-mobile': '?0',
    # 'Sec-Fetch-Dest': 'empty',
    # 'Sec-Fetch-Mode': 'cors',
    # 'Sec-Fetch-Site': 'same-origin',
    # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
    # 'X-Requested-With': 'XMLHttpRequest',
}
data = {
    # "kw":"spider",
    'from': 'en',
    'to': 'zh',
    'query': 'my',
    'transtype': 'realtime',
    'simple_means_flag': '3',
    'sign': '143310.445695',
    'token': 'c5b8d28247e217d720c16c82e8c20976',
    'domain': 'common'
}
data = urllib.parse.urlencode(data).encode('utf8')
request = urllib.request.Request(url=url, headers=headers, data=data)
response = urllib.request.urlopen(request)
content = response.read().decode('utf8')

obj = json.loads(content)
print(obj)